package org.itboys.mobile.controller.web.reward;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.reward.CooSellingReward;
import org.itboys.mobile.service.mysql.web.reward.CooSellingRewardService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.misc.Contended;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2018/1/30
 * 描述：业绩奖和全球奖
 */
@Controller
@RequestMapping("/coo/web/reward/selling")
public class CooSellingRewardController extends BaseController{
    @Autowired
    private CooSellingRewardService rewardService;
    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model){
        return "/newcoo/reward/selling/index";
    }


    /**
     * 查询
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<CooSellingReward> result=rewardService.selectReward(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 新增或者修改
     * @param request
     * @param response
     * @param reward
     */
    @RequestMapping("/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute CooSellingReward reward){
        try {
            rewardService.saveReward(reward);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/reward/selling/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

    /**
     * 编辑页面
     * @param request
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/input")
    public String input(HttpServletRequest request,
                        Model model,
                        @RequestParam(value="id",required=true) Long id){
        CooSellingReward reward = rewardService.findOne(id);
        model.addAttribute("reward",reward);
        return "/newcoo/reward/selling/input";
    }

}
